<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        #app {
            border: 2px solid black;
            padding: 10px;
            margin-bottom: 400px;
        }

        .child {
            margin: 20px;
            padding: 10px;
            background-color: #cccccc;
        }
    </style>
</head>

<body>
    <div id="app">
        <h3>父组件范围</h3>
        <child-one></child-one>
        <child-two></child-two>
    </div>
    <template id="ChildOne">
        <div class="child">
            <h3>子组件one</h3>
            <p>info:{{ info }}</p>
            <input type="text" v-model="info">
            <hr>
            <p>msg:{{ msg }}</p>
            <input type="text" v-model="msg">
        </div>
    </template>
    <template id="ChildTwo">
        <div class="child">
            <h3>子组件two</h3>
            <p>info:{{ info }}</p>
            <input type="text" v-model="info">
            <hr>
            <p>msg:{{ msg }}</p>
            <input type="text" v-model="msg">
        </div>
    </template>
</body>
<script>
    var obj={
        msg:"字符串",
        num:100,
        flag:true,
        user:{
            name:"tom"
        },
        arr:[1,2,3]
    }
    var state = Vue.observable({
        msg:"字符串",
        num:100,
        flag:true,
        user:{
            name:"tom"
        },
        arr:[1,2,3]
    });

    Vue.prototype.$state = Vue.observable({
        msg:"字符串",
        num:100,
        flag:true,
        user:{
            name:"tom"
        },
        arr:[1,2,3]
    });
    console.log(state);
    Vue.component("ChildOne", {
        template: "#ChildOne",
        data(){
            return {
                // info:state.msg
                //这样只是把值赋给info，当改变info时，并不能完成数据共享
                info:this.$state.msg
            }
        },
        computed: {
            msg:{
                get(){
                    // return obj.msg;
                    // return state.msg;
                    return this.$state.msg;
                },
                set(nv){
                    // obj.msg=nv;
                    // state.msg=nv;
                    this.$state.msg=nv;
                }
            }
        },
    });
    Vue.component("ChildTwo", {
        template: "#ChildTwo",
        data(){
            return {
                // info:state.msg
                info:this.$state.msg
            }
        },
        computed: {
            msg:{
                get(){
                    // return obj.msg;
                    // return state.msg;
                    return this.$state.msg;

                },
                set(nv){
                    // obj.msg=nv;
                    // state.msg=nv;
                    this.$state.msg=nv;

                }
            }
        },
    });

    new Vue({
        el: "#app",
    });
</script>

</html>